<?php

require_once(ROOT . LIBRARY .'/fields/common.inc');

class fields_string extends fields_common implements field
{

   /**
    * Normalize Value
    *
    * @param string $value
    * @param string $lang
    */
   public function normalizeValue($value, $lang = null)
   {
      //echo 'Translate: <b>'. $this->translate .'</b>, Alias: <b>'. $this->alias .'</b>, lang: <b>'. $lang .'</b>'; var_dump($value);
      if($this->translate)
      {
         $this->value[$lang] = stripslashes((string)$value);
      } else
      {
         $this->value = stripslashes((string)$value);
      }
      
      return $this->value;
   }

   /**
    * Get HTML code for String fields
    *
    * @param object $data
    * @return string
    */
   public function getFieldHTML($data)
   {
      if ($this->system == TRUE)
      {
         return $this->getFieldHTML_system($data);
      }

      if ($this->html == TRUE && $this->emode != 'Textarea')
      {
         return $this->getFieldHTML_editor($data);
      }

      if ($this->html && $this->emode == 'Textarea')
      {
         return $this->getFieldHTML_textarea($data);
      }

      return $this->getFieldHTML_input($data);
   }

   /**
    * Get HTML code for System String fields
    *
    * @param object $data
    * @return string
    */
   private function getFieldHTML_system($data)
   {
      $palias = !empty($this->db) ? $this->db : $this->alias;
      $value = $data->{$this->alias}(null);
      $html = '<input type="text" name="f[' . $this->alias . ']" id="f_' . $this->alias . '" value="' . htmlspecialchars($value) . '" />';

      return $html;
   }

   /**
    * Get HTML code for Editor String fields
    *
    * @param object $data
    * @return string
    */
   private function getFieldHTML_editor($data)
   {
      $html = '<textarea class="wyswyg" id="'. $this->alias .'" name="'. $this->alias .'" style="width: 100%; height: '. $this->height .'px;" data-emode="'. $this->emode .'"></textarea>';

      $langs = request::getLang('all');
      $palias = !empty($this->db) ? $this->db : $this->alias;
      foreach ($langs AS $l)
      {
         $value = $data->{$palias}($l['id']);
         $html .= '<input type="hidden" name="f[' . $this->alias . '][' . $l['id'] . ']" id="f_' . $this->alias . '_' . $l['id'] . '" value="' . htmlspecialchars($value) . '" />';
      }
      return $html;
   }

   /**
    * Get HTML code for textarea String fields
    *
    * @param object $data
    * @return string
    */
   private function getFieldHTML_textarea($data)
   {
      $langs = request::getLang('all');
      $default_lang = request::getLang('defaultId');
      $html = '';
      $palias = !empty($this->db) ? $this->db : $this->alias;

      foreach ($langs AS $l)
      {
         $value = is_object($data) ? $data->{$palias}($l['id']) : NULL;

         $html .= '<span class="multilang" rel="' . $l['id'] . '"';
         if ($l['id'] != $default_lang) $html .= ' style="display:none;"';
         $html .= '>';
         $html .= '<textarea name="f[' . $this->alias . ']';
         if(!empty($this->fieldset)) $html .= '['. (!empty($data->id) ? $data->id : $this->fieldset) .']';
         if($this->translate) $html .= '[' . $l['id'] . ']';
         $html .= '" style="width:350px;height:' . $this->height . 'px;">';
         $html .= htmlspecialchars($value) . '</textarea></span>';
      }

      return $html;
   }

   /**
    * Get HTML code for Plain String fields
    *
    * @param object $data
    * @return string
    */
   private function getFieldHTML_input($data)
   {
      if(!$this->translate)
      {
         return $this->getFieldHTML_system($data);
      }

      $langs        = request::getLang('all');
      $default_lang = request::getLang('defaultId');
      $html         = '';

      $palias = !empty($this->db) ? $this->db : $this->alias;

      foreach ($langs AS $l)
      {
         $value = is_object($data) ? $data->{$palias}($l['id']) : '';
         
         $html .= '<span class="multilang" rel="' . $l['id'] . '"';
         if ($l['id'] != $default_lang)
            $html .= ' style="display:none;"';
         $html .= '>';
         $html .= '<input type="text" name="f[' . $this->alias . ']';
         if(!empty($this->fieldset)) $html .= '['. (!empty($data->id) ? $data->id : $this->fieldset) .']';
         $html .= '[' . $l['id'] . ']" style="width:500px;" value="' . htmlspecialchars($value) . '" /></span>';
      }

      return $html;
   }


   public function validateInput($value, $objectData = NULL)
   {
      $input_value = $value;
      if($this->translate)
      {
         $tmp_value = array();
         $langs = request::getLang('all');
         foreach($langs AS $l)
         {
            if(is_array($value) && array_key_exists($l['id'], $value))
            {
               $tmp_value[$l['id']] = $this->ClearString($value[$l['id']]);
            } else
            {
               $tmp_value[$l['id']] = $this->default;
            }
         }
         
         $value = $tmp_value;
      } else
      {
         $value = $this->ClearString($value);
      }

      $this->value = $value;
      
      // JUST VALIDATE
      if(is_array($input_value)) foreach($input_value AS $k=>$v)
      {
         $input_value[$k] = $this->ClearString($v);
      } else
      {
         $input_value = $this->ClearString($input_value);
      }
      return $input_value;
   }
}
